import {
    resources,
    excelUtil as ExcelUtil
} from '../../../../../util/';
import { survey as SurveyApi } from '../../../../../config/request.js';
import echarts from 'echarts';
import { Loading } from 'element-ui';
const types = resources.eTypeName;
const eType = resources.eType;
module.exports = {
    loading: true,
    name: 'list',
    data() {
        return {
            downloadName: '分数统计-' + new Date().getTime() + '.xlsx',
            days: [],
            selectQuestion: ['眼红', '眼痒'],
            paperData: [],
            questionData: [],
            scoreDatas: [],
            countData: [{}],
            answerDetailColumn: null,
            answerDetailOption: {
                title: {
                    text: '用户详细得分情况',
                },
                tooltip: {
                    trigger: 'axis'
                },
                legend: {
                    data: types
                },
                toolbox: {
                    show: true,
                    feature: {
                        mark: { show: true },
                        dataView: { show: true, readOnly: true },
                        magicType: {
                            show: true,
                            type: ['line', 'stack', 'tiled'] // 'bar',
                        },
                        restore: { show: true },
                        saveAsImage: { show: true }
                    }
                },
                calculable: true,
                xAxis: [{
                    type: 'category',
                    data: types
                }],
                yAxis: [{
                    type: 'value',
                    axisLabel: {
                        formatter: '{value} 分'
                    }
                }],
                series: []
            },
        }
    },
    methods: {
        downloadExl() {
            this.downloadName = '分数统计-' + new Date().getTime() + '.xlsx';
            var tableData = this.paperData;
            var data = ExcelUtil.formatterData(tableData, [], { 'type': '问卷类型', 'tcount': '答卷人数' }, { 'type': ExcelUtil.formatterEtype });
            ExcelUtil.downloadExl(this.downloadName, data);
        },
        /**
         * 格式化试卷状态
         * 
         * @param {any} item 
         * @returns 
         */
        formatterType(item) {
            for (let i in eType) {
                if (item.etype == eType[i].value) {
                    return eType[i].label;
                }
            }
        },
        /**
         * 格式化试卷状态
         * 
         * @param {any} item 
         * @returns 
         */
        formatterTypeByType(type) {
            for (let i in eType) {
                if (type == eType[i].value) {
                    return eType[i].label;
                }
            }
        },
        /**
         * 过滤试卷状态
         * 
         * @param {any} value 
         * @param {any} item 
         * @returns 
         */
        filterType(value, item) {
            return item.etype == value;
        },
        handleSelectionChange() {},
        getStatistical(time) {
            SurveyApi.getStatistical.call(this, time, data => {
                if (data.code != 0) {
                    this.$message.success('请求失败！！' + data.msg);
                    return;
                }
                console.log(data)
                this.scoreDatas = [];
                for (let type in data.results) {
                    var scoreData = data.results[type];
                    if (!scoreData) {
                        continue;
                    }
                    var main = JSON.parse(scoreData.main);
                    var questions = [];
                    main.forEach(function(question) {
                        var answers = [];
                        var total = 0;
                        var count = 0;
                        for (let aid in question.answer) {
                            var answer = question.answer[aid];
                            Object.assign(answer, { id: aid });
                            count += Number(answer.count);
                            total += (Number(answer.count) * Number(answer.score));
                            answers.push(answer);
                        }
                        var average = Math.round(total / count * 100) / 100.00;

                        questions.push({ main: question.question, answers: answers, average: average })

                    }, this);

                    this.scoreDatas.push({
                        tcount: scoreData.tcount,
                        type: scoreData.type,
                        time: scoreData.time,
                        main: scoreData.main,
                        questions: questions
                    });
                }

                this._setPaperData();
                this._setEchartsOption();
            });
        },
        _setPaperData() {
            this.questionData = [];
            this.paperData = [];
            this.scoreDatas[0].questions.forEach(question => {
                this.questionData.push(question.main);
            });
            this.scoreDatas.forEach(scoreData => {
                var paper = {};
                var total = 0;
                scoreData.questions.forEach(question => {
                    total += Number(question.average);
                    paper[question.main] = question.average;
                })

                paper['平均分'] = Math.round(total / this.questionData.length * 100) / 100.00;
                Object.assign(paper, {
                    type: scoreData.type,
                    tcount: scoreData.tcount,
                });

                this.paperData.push(paper);
            })

        },
        _setEchartsOption() {
            var options = [];
            var indexs = [];
            var selects = this.selectQuestion;
            selects.forEach(select => {
                options.push({
                    name: select,
                    type: 'line',
                    stack: '总分',
                    data: []
                });
            })
            selects.forEach(select => {
                indexs.push(this.questionData.indexOf(select));
            });
            this.scoreDatas.forEach(scoreData => {
                var questions = scoreData.questions;
                for (let i = 0, length = indexs.length; i < length; i++) {
                    options[i].data.push(questions[indexs[i]].average);
                }
            }, this);
            this.answerDetailColumn = echarts.init(document.getElementById('answerDetailColumn'));
            this.answerDetailOption.series = options;
            this.answerDetailColumn.setOption(this.answerDetailOption);
        },
        statisticalData() {
            SurveyApi.statisticalAll.call(this, data => {
                if (data.code == 0) {
                    this.$message.success('处理完成');
                } else {
                    this.$message.success('请求失败！！' + data.msg);
                }
            });
        },
        formatterTime(time) {
            return new Date(time * 1000).toLocaleString();
        },
        _initDays() {
            this.days = [];
            var time = new Date().getTime();
            for (let i = 5; i >= 0; i--) {
                var value = time - i * 1000 * 3600 * 24;
                this.days.push({ value: value, label: new Date(value).toLocaleDateString() });
            }
        }
    },
    created() {
        this.getStatistical(new Date().getTime());
        this._initDays();
    },
    mounted() {}
}